<template>
  <el-dialog title="余额充值" :visible.sync="dialogVisible" :close-on-click-modal="false" width="800px" top="50px" append-to-body>
    <!-- 余额充值弹出 -->
    <div class="form-box">

      <div class="kuaijie flex ">
        <div class="kuaijie-item" @click="kuaijieClick(item.price, i)" :class="{ active: i === activeIndex }" v-for="(item, i) in huodong" :key="i">
          <div class="money font18">{{item.price }}元</div>

          <!-- <div class="xs">限时</div> -->
        </div>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="135px" class="demo-ruleForm">
        <el-form-item label="自定义充值金额:" prop="price">
          <el-input-number :controls="false" v-model="form.price" placeholder="请输入充值金额" @change="getCountInfoByType" />元
        </el-form-item>
        <el-form-item label="支付方式：" prop="num">
          <payWayLsit :showYue="false" :payWay='payWay' ref="payWayLsit" @selectPayway="selectPayway"></payWayLsit>
        </el-form-item>

        <div class="tip">
          说明：充值金额直接到你的账户余额，提现时需要1%手续费。
        </div>

        <div class="center">
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">确认充值</el-button>
        </div>
      </el-form>
      <payQrcode ref="payQrcode" @checkOrderInterval="checkOrderInterval" @checkOrder="checkOrder"></payQrcode>
    </div>
  </el-dialog>
</template>
<script>
import moneyApi from "@/api/financeCenter/myMoney";
import payApi from "@/api/financeCenter/pay";
import payQrcode from "./PayQrcodeCommon.vue";
import payWayLsit from "../../components/payWayLsit.vue";
import _ from "lodash";
export default {
  name: "addjifen",
  components: { payQrcode, payWayLsit },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      activeIndex: "",
      activeInfo: null,
      countJifen: "",
      payWay: 2,
      huodong: [
        {
          price: 500,
        },
        {
          price: 1000,
        },
        {
          price: 5000,
        },
      ],
      activeRemark: "",
      // 表单参数
      form: {
        price: "",
      },
      orderId: "",
      rules: {
        price: [{ required: true, message: "请输入充值金额", trigger: "blur" }],
      },
      jieliu: 0,
    };
  },
  created() {},
  methods: {
    // 打开弹窗
    open() {
      this.form.price = this.huodong[0].price;
      this.loading = false;
      this.dialogVisible = true;
      this.activeIndex = 0;

      // this.activitySettingsGetByType();
    },
    // // 获取活动信息
    // activitySettingsGetByType() {
    //   moneyApi
    //     .activitySettingsGetByType({
    //       type: 1,
    //     })
    //     .then((res) => {
    //       if (res.code == 200) {
    //         // 将/n换成<br/>
    //         let data = res.data;
    //         this.activeRemark = data.remark.replace(/\n/g, "<br/>");
    //         this.activeInfo = data;
    //         this.huodong = JSON.parse(data.rule);
    //         this.form.price =
    //           this.activeInfo && this.activeInfo.mode == 1
    //             ? this.huodong[0].minAmount
    //             : this.huodong[0].maxAmount;
    //         this.countComputed();
    //       }
    //     });
    // },
    selectPayway(val) {
      this.payWay = val;
    },
    // 根据金额计算应得虞豆
    getCountInfoByType(val) {
      // if (!val) {
      //   this.countJifen = 0;
      //   return;
      // }
      // if (val < 10) {
      //   return this.$message.error("充值金额不能小于10元！");
      // }
      // if (this.jieliu) {
      //   clearTimeout(this.jieliu);
      // }
      // this.jieliu = setTimeout(() => {
      //   this.countComputed();
      // }, 500);
    },
    countComputed() {
      moneyApi.getCountInfoByType({ price: this.form.price }).then((res) => {
        this.countJifen = res.data.count;
        this.jieliu = 0;
      });
    },

    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    kuaijieClick(money, i) {
      this.activeIndex = i;
      this.form.price = money;
      this.$refs["form"].validate();
      // this.countComputed();
    },
    // 确认充值
    async submitForm(formName) {
      // if (this.form.price < 10) {
      //   return this.$alert("充值金额不能小于10元！");
      // }
      this.$refs["" + formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$confirm(`确认充值${this.form.price}元到余额吗？`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
          
              moneyApi
                .addBzj({
                  orderType: 2,
                  amount: this.form.price,
                })
                .then(async (res) => {
                  this.orderId = res.data.id;
                  await this.payNow(res.data.id);
                  this.loading = false;
                });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          this.$alert("请输入充值金额！");
        }
      });
    },

    // 轮询订单支付结果
    checkOrderInterval() {
      moneyApi.payBzjStateInterval({ id: this.orderId }).then((res) => {
        if (res.code == 200) {
          if (res.data.pay_status == 2) {
            this.$store.dispatch("getShopInfo");
            this.$refs.payQrcode.showSuccess(
                "余额",
                "充值成功！"
              );
              this.$refs.payQrcode.close();
            this.dialogVisible = false;
          }
        }
      });
    },
    // 查询订单状态
    checkOrder() {
      moneyApi
        .payBzjStateHander({
          id: this.orderId,
        })
        .then((res) => {
          if (res.code == 200) {
            if (res.data.payStatus == 2) {
              this.$store.dispatch("getShopInfo");
              this.$refs.payQrcode.showSuccess(
                "余额",
                "充值成功！"
              );
              this.$refs.payQrcode.close();
              this.dialogVisible = false;
            } else {
              this.$message.waring("未查询到支付结果！如有问题请联系客服");
            }
          }
        });
    },
    // 请求支付参数
    async payNow(id) {
      let obj = {
        id: id,
        payWay: this.payWay,
        payScene: 3,
        qrPayMode: 4,
        goodsTitle: "余额充值",
      };
      const res1 = await payApi.userPayOrderRecordPayment(obj);
      //console.log(res1);
      if (res1.code === 200) {
        const isHuifu = res1.data.isHuifu||1
        this.$refs.payQrcode.open(this.payWay,res1.data.formStr,isHuifu )
      }
    },

    close() {
      this.reset();
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.form-box {
  padding: 10px 10px;
  .top-box {
    .flex {
      align-items: center;
    }
    flex-wrap: wrap;
    .item {
      flex-shrink: 0;
      width: 217px;
      height: 110px;
      margin-right: 20px;
      background-image: url("https://imgage.maimaihao.com/yuduhulian/erp/static/icon/jf-bg.png");
      background-size: 100% 100%;
      padding: 5px 30px 0 10px;
      margin-bottom: 10px;
      position: relative;
      &:nth-of-type(3n) {
        margin-right: 0;
      }
      .money {
        color: #fff;
      }

      .clock {
        width: 18px;
        height: 18px;
        background-image: url("https://imgage.maimaihao.com/yuduhulian/erp/static/icon/clock.png");
        background-size: 100% 100%;
        margin-right: 3px;
      }
      .xs {
        font-size: 16px;
        font-weight: 800;
        color: #333333;
        background: linear-gradient(180deg, #f2e36c 0%, #d1a80d 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        line-height: 16px;
      }
      .name {
        position: absolute;
        right: 0;
        bottom: 0;
        width: 60px;
        height: 18px;
        background: #ffffff;
        border-radius: 4px 0px 9px 0px;
        border: 1px solid #cbcafb;
        text-align: center;
        color: #ff0000;
        font-size: 12px;
      }
      .item-bot {
        color: #000;
        margin-top: 20px;

        .jf {
          font-size: 24px;
          font-weight: 800;
        }
      }
    }
  }
  .des {
    margin-top: 10px;
    .tit {
      font-weight: bold;
    }
  }

  .sub-tit {
    font-size: 24px;
    margin-top: 10px;
  }
  .kuaijie {
    margin-top: 20px;
    flex-wrap: wrap;
  }
  .kuaijie-item {
    min-width: 200px;
    // width: 100%;
    // height: 49px;
    border-radius: 4px;
    border: 1px solid $yellow;
    text-align: center;
    color: $yellow;
    padding: 10px;
    margin-right: 30px;
    margin-bottom: 10px;
    cursor: pointer;
    position: relative;
    &.active {
      color: #fff;
      background-color: #ff9717;
    }
    .xs {
      background-color: #eb8200;
      width: 33px;
      height: 17px;
      font-size: 12px;
      position: absolute;
      top: 0;
      right: 0;
      color: #fff;
    }
  }
  .demo-ruleForm {
    margin-top: 20px;
  }
  .zfb {
    width: 42px;
    height: 42px;
  }
  .tip {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.5);
    margin-top: -15px;
  }
  .center {
    text-align: center;
    margin-top: 20px;
    .el-button {
      width: 293px;
      height: 48px;
      font-size: 18px;
    }
  }
}
</style>
